/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.yurendao.OSFileSystem.myTree;

import java.util.ArrayList;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Administrator
 * //此类 用于对操作过程中产生的 各种String  进行处理
 * //以转换成 需要的数据类型
 */
public class ChangeString {

    //将从 JTree上得到的 选中结点的路径,如：[root,test,demo1]
    //转换成 ArrayList数组
    public static ArrayList toFilePath(String filepath){
        ArrayList files = new ArrayList();
        String filepath0 = filepath.replace("[", ", ");
        String filepath1 = filepath0.replace("]", ", ");
        String[] tokens = filepath1.split(", ");
        for(int i=1;i<tokens.length;i++){
            files.add(tokens[i]);
        }
        return files;
    }

    public static String toPath(String filepath){
        String path = "";
        String filepath0 = filepath.replace("[", ", ");
        String filepath1 = filepath0.replace("]", ", ");
        String[] tokens = filepath1.split(", ");
        if(tokens.length<=2){
             path = tokens[1] + ":\\";
        }else{
            path = tokens[1] + ":";
            for (int i = 2; i < tokens.length; i++) {
                path = path + "\\" + tokens[i];
            }
        }
        return path;
    }

    //应该 认定要添加的结点是TreeNode
    //因为 TreePath 的 getPath()方法 得到的是 （TreeNode[]）
    public static TreePath toTreePath(TreePath filepath,DefaultMutableTreeNode treenode){
        //ArrayList allpath = (ChangeString.toFilePath(filepath+treenode));
        TreePath treepath = filepath.pathByAddingChild(treenode);
        return treepath;
    }

}
